package com.example.demo.controller;

import com.example.demo.common.HTTPResult;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.entity.Commentinfo;
import com.example.demo.entity.Userinfo;
import com.example.demo.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    // 根据文章 id 获取评论信息
    @RequestMapping("/commentinfo")
    public HTTPResult getComments(HttpServletRequest request, Integer id, Integer comment_level, Integer commentId) {
        Userinfo userinfo = UserSessionUtils.getUserBySession(request);
        if(userinfo == null) {
            return HTTPResult.fail(-1, "非法请求");
        }
        // 获取首评论
        if(comment_level == 0) {
            return HTTPResult.success(commentService.getCommentList(id, comment_level, null));
        }
        // 获取子评论
        // 判断父评论状态是否为1
        if(commentService.getComment(commentId).getChild_status().equals(0)) {
            // 父评论没有子评论，直接返回异常
            HTTPResult.fail(-2, "该评论没有子评论!");
        }
        return HTTPResult.success(commentService.getCommentList(id, comment_level, commentId));
    }

    // 查询评论总数量
    @RequestMapping("/count")
    public HTTPResult commentCount(HttpServletRequest request) {
        // 1. 获取当前登录用户
        Userinfo userinfo = UserSessionUtils.getUserBySession(request);
        if(userinfo == null || userinfo.getUsertype() != 1) {
            return HTTPResult.fail(-1, "非法请求");
        }
        // 得到用户发表文章的总数
        int commentCount = commentService.getCommentCount(null);
        return HTTPResult.success(commentCount);
    }

    // 新增文章评论
    @RequestMapping("/submit")
    public HTTPResult submit(HttpServletRequest request, String content, Integer id) {
        Userinfo userinfo = UserSessionUtils.getUserBySession(request);
        if(userinfo == null || id == null || content == null) {
            return HTTPResult.fail(-1, "非法请求");
        }
        Commentinfo commentinfo = new Commentinfo();
        // 添加文章id
        commentinfo.setContent_id(id);
        // 添加文章正文
        commentinfo.setComment_details(content);
        // 添加登录用户id，即评论的人的id
        commentinfo.setUser_id(userinfo.getId());
        return HTTPResult.success(commentService.addComment(commentinfo));
    }

    // 新增评论的子评论
    @RequestMapping("/submitchild")
    public HTTPResult submitchild(HttpServletRequest request, Integer id, String content, Integer parent_id) {
        Userinfo userinfo = UserSessionUtils.getUserBySession(request);
        if(userinfo == null || id == null || content == null) {
            return HTTPResult.fail(-1, "非法请求");
        }
        Commentinfo commentinfo = new Commentinfo();
        // 添加文章id
        commentinfo.setContent_id(id);
        // 添加文章正文
        commentinfo.setComment_details(content);
        // 添加登录用户id，即评论的人的id
        commentinfo.setUser_id(userinfo.getId());
        // 添加父评论id
        commentinfo.setParent_id(parent_id);
        return HTTPResult.success(commentService.addCommentChild(commentinfo));
    }

    // 删除评论
    @RequestMapping("/delete")
    public HTTPResult delete(HttpServletRequest request, Integer commentId) {
        if(commentId == null || commentId <= 0) {
            // 参数有误
            return HTTPResult.fail(-1, "参数异常");
        }
        Userinfo userinfo = UserSessionUtils.getUserBySession(request);
        if(userinfo == null) {
            return HTTPResult.fail(-2, "用户未登录");
        }
        Integer uid = userinfo.getId();
        Commentinfo commentinfo = commentService.getComment(commentId);
        if(userinfo.getUsertype() == 1) {
            // 管理员身份，直接删除
            return HTTPResult.success(commentService.delComment(commentId, null));
        }
        if((!commentinfo.getUser_id().equals(uid))) {
            return HTTPResult.fail(-3, "该评论非该角色发布！");
        }
        return HTTPResult.success(commentService.delComment(commentId, uid));
    }


}
